package com.module.framework.handler.security;

import com.alibaba.fastjson.JSON;
import com.module.framework.domain.ResponseResult;
import com.module.framework.enums.AppHttpCodeEnum;
import com.module.framework.utils.WebUtils;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author JiHuiQin
 * @Date 2022/2/7 18:10
 * @Version 1.0
 */

@Component
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {
	/**
	 * 异常处理，授权失败
	 *
	 * @param request
	 * @param response
	 * @param e
	 * @throws IOException
	 * @throws ServletException
	 */
	@Override
	public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException e) throws IOException, ServletException {
		e.printStackTrace();
		ResponseResult result = ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH);
		String json = JSON.toJSONString(result);
		WebUtils.renderString(response, json);
	}
}
